package com.cwt_taxi_xa.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class GPIOUtil {
	/**
	 * 控制设备
	 *
	 * @param state
	 * @param filePath
	 */
	static File file;
	public static void writeToDevice(int state, String filePath) {
		file = new File(filePath);
		FileWriter outputStream = null;
		try {
			outputStream = new FileWriter(file);
			if (state == 1) {
				outputStream.write("1");
			} else if (state == 0) {
				outputStream.write("0");
			}
			// outputStream.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (outputStream != null) {
					outputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	static FileReader localFileReader;
	static BufferedReader localBufferedReader;
	public static int readIOState(String path) {
		String ioState = "";
		try {
			localFileReader = new FileReader(path);
			localBufferedReader = new BufferedReader(localFileReader, 1);
			ioState = localBufferedReader.readLine();
			localBufferedReader.close();
			localFileReader.close();
		} catch (IOException e) {
			ioState = "-1";
		}
		int result = -1;
		if (ioState == null || ioState.equals("")) {
			result = -1;
		} else if (ioState.equals("1")) {
			result = 1;
		} else if (ioState.equals("0")) {
			result = 0;
		} else if (ioState.equals("-1")) {
			result = -1;
		}
		return result;
	}

}
